#ifndef LOADDISCTITLEBYIDRESPONSEPARSER_H
#define LOADDISCTITLEBYIDRESPONSEPARSER_H

#include <QXmlDefaultHandler>
#include <QDebug>
#include "disctitle.h"

class LoadDiscTitleByIdResponseParser : public QXmlDefaultHandler
{
public:
    explicit LoadDiscTitleByIdResponseParser();
    
    bool startDocument()
    {
        _isTitle = _isAudioTracks = _isSubtitles = _isStudios = _isDiscs = _isPersons = false;
        return true;
    }

    bool startDTD(const QString &name, const QString &publicId, const QString &systemId)
    {
        qDebug() << "startDTD(const QString &name, const QString &publicId, const QString &systemId)";
        return true;
    }
    bool startCDATA()
    {
        qDebug() << "startCDATA()";
        return true;
    }

    bool isTitleLevel()
    {
        return _isTitle && !_isAudioTracks && !_isSubtitles && !_isStudios && !_isDiscs && !_isPersons && !_isParentalRating && !_isCovers;
    }

    bool characters(const QString &ch)
    {
        qDebug() << "characters(const QString &ch)";
        qDebug() << "ch: "<<ch;

        if(isTitleLevel()&&_currentName=="ID")
        {
            this->_discTitle.id = ch;
        }
        else if(isTitleLevel()&&_currentName=="Type")
        {
            this->_discTitle.type = ch;
        }
        else if(isTitleLevel()&&_currentName=="MediaType")
        {
            this->_discTitle.mediaType = ch;
        }
        else if(isTitleLevel()&&_currentName=="Description")
        {
            this->_discTitle.description = ch;
        }
        else if(isTitleLevel()&&_currentName=="LocalTitle")
        {
            this->_discTitle.localTitle = ch;
        }
        else if(isTitleLevel()&&_currentName=="OriginalTitle")
        {
            this->_discTitle.originalTitle = ch;
        }
        else if(isTitleLevel()&&_currentName=="SortTitle")
        {
            this->_discTitle.sortTitle = ch;
        }
        else if(_isCovers)
        {
            if(_currentName=="Front")
            {
                 this->_discTitle.cover = ch;
            }
        }
        else if(_isDiscs)
        {
            if(_isDisc)
            {
                if(_currentName=="DoubleSided")
                {
                    _discAux.doubleSided = ch;
                }
                else if(_currentName=="DiscIdSideA")
                {
                    _discAux.discIdSideA = ch;
                }
                else if(_currentName=="DiscIdSideB")
                {
                    _discAux.discIdSideB = ch;
                }
                else if(_currentName=="Name")
                {
                    _discAux.name = ch;
                }
            }

        }
        else
        {
            qDebug() << "unhandler element";
        }
        return true;
    }
    void setElement(const QString &qName,bool value)
    {
        if(qName=="Title")
        {
            _isTitle = value;
        }
        else if(qName=="AudioTracks")
        {
            _isAudioTracks = value;
        }
        else if(qName=="Subtitles")
        {
            _isSubtitles = value;
        }
        else if(qName=="Studios")
        {
            _isStudios = value;
        }
        else if(qName=="Studio")
        {
            _isStudio = value;
        }
        else if(qName=="Discs")
        {
            _isDiscs = value;
        }
        else if(qName=="Disc")
        {
            _isDisc = value;
        }
        else if(qName == "ParentalRating")
        {
            _isParentalRating = value;
        }
        else if(qName == "Covers")
        {
            _isCovers = value;
        }
        else
        {
            qDebug()<<"unhandler element: "<<qName;
        }

    }

    bool startElement( const QString& namespaceURI, const QString& localName, const QString &qName, const QXmlAttributes &attrs )
    {
        qDebug() << "startElement";
        qDebug() << "name:" + qName;
        qDebug() << "localName:" + localName;
        qDebug() << "namespaceURI:" + namespaceURI;
        _currentName = qName;
        setElement(qName,true);

        if(_isDiscs&&qName=="Disc")
        {

            _discAux = Disc();
            for( int i=0; i<attrs.count(); i++ )
            {
                qDebug()<<"attrs "<<i<<" :"<< attrs.value( i );
                qDebug()<<"attrs qName "<<i<<" :"<< attrs.qName( i );
                qDebug()<<"attrs type "<<i<<" :"<< attrs.type(i );
                switch(i)
                {
                case 0:
                    _discAux.titleMode= attrs.value( i );
                    break;
                case 1:
                    _discAux.bonusDisc= attrs.value( i );
                    break;
                case 2:
                    _discAux.digitalCopy= attrs.value( i );
                    break;
                default:
                    qDebug()<<"Unknow attribute index: "<<i;
                    break;
                }

            }
        }
        if(isTitleLevel())
        {
            for( int i=0; i<attrs.count(); i++ )
            {
                qDebug()<<"attrs "<<i<<" :"<< attrs.value( i );
                qDebug()<<"attrs qName "<<i<<" :"<< attrs.qName( i );
                qDebug()<<"attrs type "<<i<<" :"<< attrs.type(i );
                switch(i)
                {
                case 0:
                    this->_discTitle.bluRay3D= attrs.value( i );
                    break;
                case 1:
                    this->_discTitle.boxSet= attrs.value( i );
                    break;
                default:
                    qDebug()<<"Unknow attribute index: "<<i;
                    break;
                }
            }
        }
        return true;
    }
    bool endElement( const QString& namespaceURI, const QString& localName, const QString &qName )
    {
        qDebug() << "endElement: ";

        qDebug() << "namespaceURI: "<<namespaceURI;
        qDebug() << "localName: "<<localName;
        qDebug() << "qName: "<<qName;
        if(_isDiscs&&qName=="Disc")
        {
            _discTitle.discs.append(_discAux);
        }
        setElement(qName,false);

        return true;
    }

    DiscTitle getDiscTitle() const;
private:
     DiscTitle _discTitle;
     QString _currentName;
     bool _isTitle;
     bool _isAudioTracks;
     bool _isSubtitles;
     bool _isStudios;
     bool _isStudio;
     bool _isDiscs;
     bool _isDisc;
     bool _isPersons;
     bool _isPerson;
     bool _isParentalRating;
     bool _isCovers;
     Disc _discAux;


};

#endif // LOADDISCTITLEBYIDRESPONSEPARSER_H
